home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / HYP / C-D / DartmouthXCMDs.cpt / Dartmouth XCMDs Vol 1&2 / card_10186.txt < prev    next >
Text File  |  1989-02-26  |  12KB  |  443 lines

  1. -- card: 10186 from stack: in
  2. -- bmap block id: 10283
  3. -- flags: 0000
  4. -- background id: 8327
  5. -- name: FindInField
  6. ----- HyperTalk script -----
  7. on opencard
  8.   set the visible of field 1 to false -- background field
  9.   set the scroll of card field 1 to 0
  10. end opencard
  11. on closecard
  12.   set the visible of field 1 to true
  13. end closecard
  14.  
  15. on showIt pos,theLength
  16.   put item 1 of the rect of card field 1 into x
  17.   put item 2 of the rect of card field 1 into y
  18.   put item 3 of pos into theChar
  19.   put item 2 of pos into theLine
  20.   put the textHeight of card field 1 into th
  21.   put 6 into ts  -- courier
  22.   put y+(theLine*th) into y
  23.   put the scroll of card field 1 into theScroll
  24.   get the rect of card field 1
  25.   put item 4 of it - item 2 of it into theHeight
  26.   if (theScroll+theHeight) < y then
  27.     set the scroll of card field 1 to (y-10*th)
  28.   end if
  29.   put x+(theChar*ts) into x
  30.   put y - the scroll of card field 1 into y
  31.   put x into item 1 of theLoc1
  32.   put y into item 2 of theLoc1
  33.   put x + theLength*ts into item 1 of theLoc2
  34.   put y into item 2 of theLoc2
  35.   click at theloc1  -- this causes the whole word to be selected
  36.   drag from theLoc1 to theLoc2 -- but otherwise I only get an
  37.   --                              insertion bar!
  38.   wait for 1 seconds
  39. end showIt
  40.  
  41.  
  42. -- part 5 (field)
  43. -- low flags: 00
  44. -- high flags: 2007
  45. -- rect: left=18 top=32 right=290 bottom=384
  46. -- title width / last selected line: 0
  47. -- icon id / first selected line: 0 / 0
  48. -- text alignment: 0
  49. -- font id: 22
  50. -- text size: 10
  51. -- style flags: 0
  52. -- line height: 13
  53. -- part name: XCMD Text
  54.  
  55.  
  56. -- part 6 (button)
  57. -- low flags: 00
  58. -- high flags: 8003
  59. -- rect: left=304 top=299 right=321 bottom=425
  60. -- title width / last selected line: 0
  61. -- icon id / first selected line: 0 / 0
  62. -- text alignment: 1
  63. -- font id: 0
  64. -- text size: 12
  65. -- style flags: 0
  66. -- line height: 16
  67. -- part name: Show LSC Source
  68. ----- HyperTalk script -----
  69. on mouseUp
  70.   get the visible of card field "source"
  71.   set the visible of card field "source" to not it
  72.   if it is false then
  73.     set the name of me to "Hide LSC Source"
  74.   else
  75.     set the name of me to "Show LSC Source"
  76.   end if
  77. end mouseUp
  78.  
  79.  
  80.  
  81. -- part 9 (field)
  82. -- low flags: 00
  83. -- high flags: 0004
  84. -- rect: left=389 top=268 right=289 bottom=483
  85. -- title width / last selected line: 0
  86. -- icon id / first selected line: 0 / 0
  87. -- text alignment: 0
  88. -- font id: 3
  89. -- text size: 12
  90. -- style flags: 0
  91. -- line height: 16
  92. -- part name: found
  93.  
  94.  
  95. -- part 10 (button)
  96. -- low flags: 00
  97. -- high flags: 2000
  98. -- rect: left=395 top=76 right=111 bottom=480
  99. -- title width / last selected line: 0
  100. -- icon id / first selected line: 0 / 0
  101. -- text alignment: 1
  102. -- font id: 0
  103. -- text size: 12
  104. -- style flags: 0
  105. -- line height: 16
  106. -- part name: Not case
  107. ----- HyperTalk script -----
  108. on mouseUp
  109.   ask "Find what?"
  110.   put it into myTarget
  111.   if myTarget is not empty then
  112.     set the scroll of card field 1 to 0
  113.     get FindInField(card field 1,myTarget,"true",0)
  114.     put it into card field "found"
  115.     showIt it,the number of chars in myTarget
  116.   end if
  117. end mouseUp
  118.  
  119.  
  120.  
  121. -- part 11 (button)
  122. -- low flags: 00
  123. -- high flags: 2000
  124. -- rect: left=395 top=123 right=158 bottom=480
  125. -- title width / last selected line: 0
  126. -- icon id / first selected line: 0 / 0
  127. -- text alignment: 1
  128. -- font id: 0
  129. -- text size: 12
  130. -- style flags: 0
  131. -- line height: 16
  132. -- part name: New Button
  133. ----- HyperTalk script -----
  134. on mouseUp
  135.   ask "Find what?"
  136.   put it into myTarget
  137.   if myTarget is not empty then
  138.     set the scroll of card field 1 to 0
  139.     get FindInField(card field 1,myTarget,"",0)
  140.     put it into card field "found"
  141.     showIt it,the number of chars in myTarget
  142.   end if
  143. end mouseUp
  144.  
  145.  
  146.  
  147. -- part 12 (button)
  148. -- low flags: 00
  149. -- high flags: 2000
  150. -- rect: left=395 top=169 right=215 bottom=480
  151. -- title width / last selected line: 0
  152. -- icon id / first selected line: 0 / 0
  153. -- text alignment: 1
  154. -- font id: 0
  155. -- text size: 12
  156. -- style flags: 0
  157. -- line height: 16
  158. -- part name: New Button
  159. ----- HyperTalk script -----
  160. on mouseUp
  161.   ask "Show what?"
  162.   if it is not empty then
  163.     hide message
  164.     set the scroll of card field 1 to 0
  165.     put it into target
  166.     put false into done
  167.     put 0 into start
  168.     --put empty into msg
  169.     repeat while done is false
  170.       get FindInField(card field 1,target,"",start)
  171.       if item 1 of it is 0 then
  172.         put true into done
  173.       else
  174.         put it into card field "found"
  175.         showIt it,the number of chars in target
  176.         put item 1 of it + 1 into start
  177.       end if
  178.     end repeat
  179.   end if
  180. end mouseUp
  181.  
  182.  
  183.  
  184.  
  185. -- part 14 (button)
  186. -- low flags: 00
  187. -- high flags: 0000
  188. -- rect: left=395 top=46 right=64 bottom=468
  189. -- title width / last selected line: 0
  190. -- icon id / first selected line: 0 / 0
  191. -- text alignment: 1
  192. -- font id: 0
  193. -- text size: 12
  194. -- style flags: 0
  195. -- line height: 16
  196. -- part name: New Button
  197.  
  198.  
  199. -- part 7 (field)
  200. -- low flags: 81
  201. -- high flags: 0007
  202. -- rect: left=18 top=31 right=290 bottom=489
  203. -- title width / last selected line: 0
  204. -- icon id / first selected line: 0 / 0
  205. -- text alignment: 0
  206. -- font id: 3
  207. -- text size: 10
  208. -- style flags: 0
  209. -- line height: 13
  210. -- part name: source
  211.  
  212.  
  213. -- part contents for card part 5
  214. ----- text -----
  215. FindInField version 1.3
  216. Roger Brown
  217.  
  218. The FindInField XFCN locates a string in a given field 
  219. or container as fast as HyperCard can.
  220.  
  221.  It searches only the specified container.  
  222.  
  223. You can specify a starting position and whether or not 
  224. the search is case sensitive.
  225.  
  226.  It returns information on the location of the find: 
  227.     item 1 is the byte position in the string (1-based)
  228.     item 2 is the line number in the string
  229.     item 3 is the byte position in the line (1-based)
  230.  
  231.       --- all are zero if the string is not found
  232.    
  233.  
  234. INVOKING FindInField
  235.  
  236.   get FindInField(field,target,"true",start)
  237.  
  238.      where   field   is the name of the field or          
  239.                      container to search
  240.              target  is the string to search for
  241.             "true"   is an option parameter to indicate                 
  242.                      case sensitivity for the search
  243.              start   is the (optional) starting byte 
  244.                      offset
  245.                    
  246.  
  247. EXAMPLE
  248.  
  249.  ex.  get FindInField(card field 1,"EXAMPLE","true",0)
  250.  
  251.  
  252. REVISION HISTORY
  253. 1.1  4/31/88 - FindInField using HCLib was way too slow
  254. 1.2  5/5/88  - rewrite in more "C" style, one pass over 
  255.                the field
  256. 1.3  5/10/88 - add case sensitivity and starting 
  257.                position
  258.  
  259. -- part contents for card part 7
  260. ----- text -----
  261. /* QuickFindInField1.3.c */
  262. /* ¬© Trustees of Dartmouth College */
  263. /* written in LightSpeed C  ¬© Think Technologies, Inc */
  264. /* by Roger Brown 5/10/88  Courseware Development Group */
  265. /* add start byte input as last parameter */
  266. /* This is a HyperCard XFCN that locates a target string in an input
  267.    field (or other container) and returns the byte position in the field, the number 
  268.    of the line where it was find, and the byte position in that line.
  269.    Case sensitivity is used if an optional third parameter is "true". 
  270.    Syntax is:
  271.    
  272.         get FindInField(container,target,"true")
  273.    
  274.    ex.      get FindInField(card field 1,"***")
  275.   
  276.    returns: item 1: byte offset in field or 0 if not found
  277.             item 2: line of card field 1 that contains '***' or 0
  278.             item 3: byte position in the line or 0
  279.    
  280.    where    container   is any hypercard container (field, variable), presumed to be 
  281.                         multi-lined.
  282.             target      is the string to look for   
  283.             true        is an optional parameter turning on case sensitivity   
  284.                    
  285.       To compile: create a project with this and MacTraps. Build as code resource type
  286.    XFCN named FindInField.
  287. */
  288.  
  289.  
  290.  
  291. #include "stddata_ctype.c"
  292. #include "strings.c"
  293. #include "ToolboxUtil.h"
  294. #include "HyperXCmd.h"
  295. #include "XCmdGlue.inc.c"
  296.  
  297. /* change a string to all upper case */
  298.  
  299. int toupper(c)
  300. char    c;
  301. {
  302.     return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  303. }
  304.  
  305. /* see if two characters match, maybe ignoring case */
  306.  
  307. matchChar(a,b,theCase)
  308. char a,b;
  309. int theCase;
  310. {
  311.     if (theCase==0) return (a==b);
  312.     return (toupper(a)==toupper(b));
  313. }
  314.  
  315. /* build the return result string from numeric parts */
  316.  
  317. BuildResult(fPos,fLine,lPos,result)
  318. int fPos,fLine,lPos;
  319. char *result;
  320. {
  321.     char temp[64];
  322.     
  323.     NumToString((long)fPos,temp);
  324.     PtoCstr(temp);
  325.     strcpy(result,temp);
  326.     strcat(result,",");
  327.     NumToString((long)fLine,temp);
  328.     PtoCstr(temp);
  329.     strcat(result,temp);
  330.     strcat(result,",");
  331.     NumToString((long)lPos,temp);
  332.     PtoCstr(temp);
  333.     strcat(result,temp);
  334. }
  335.  
  336. /* find the target as specified */
  337.  
  338. FindIt(theField,theTarget,caseSens,result,start)
  339. char *theField;          /* pointer to field to search */
  340. char *theTarget;         /* pointer to target string */
  341. char *result;            /* pointer to the return result string */
  342. int caseSens,start;      /* case sensitive = 1 for yes, start is where (byte) to start */
  343. {
  344.     int c;             /* index of current character */
  345.     int tLen;          /* target length */
  346.     int line;          /* line currently being searched */
  347.     int lPos;          /* character position in line */
  348.     register char *fc,*tc; /* pointers to field and target characters */
  349.     
  350.     tLen = strlen(theTarget);         /* assume this is short */
  351.     c = lPos = 0; 
  352.     line = 1;                            /* offset into theField */
  353.     fc = theField;                       /* pointer into theField */
  354.     while (*fc != 0) {                   /* scan to end */
  355.         tc = theTarget;                  /* pointer to theTarget */
  356.         if (*fc==13) {
  357.              line++;                     /* passing a line boundary */
  358.              lPos = -1;                  /* reset position */
  359.         }
  360.         if ((c+1) > start) {             /* don't go until something matches */
  361.             while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  362.                 fc++;                      /* cycle through matching characters */
  363.                 tc++;
  364.                 if (tc == (theTarget+tLen)){   /* all target characters matched */
  365.                     BuildResult(c+1,line,lPos+1,result);
  366.                      return;  /* matched */
  367.                 }
  368.             }
  369.         }
  370.         c++;                          /* move along */
  371.         lPos++;
  372.         fc = theField + c;            /* reset pointer after a search */
  373.     }
  374.     BuildResult(0,0,0,result);
  375.     return;                         /* target not found */
  376. }
  377.  
  378. /* XCMD entry */
  379.  
  380. pascal void FindInField(paramPtr)
  381. XCmdBlockPtr    paramPtr;
  382. {   
  383.     int i;
  384.     Str255 theResult,caseStr,startStr;
  385.     Ptr theField,theTarget,casePtr,theStart;
  386.     long line;
  387.     Size len;
  388.     CursHandle theCursor;
  389.     Handle resultHandle;
  390.     int theCase;
  391.     long startAt;
  392.     
  393.     theCursor = GetCursor(watchCursor);
  394.     SetCursor(*theCursor);
  395.     for (i=0;i<paramPtr->paramCount;i++) {
  396.         MoveHHi(paramPtr->params[i]);
  397.         HLock(paramPtr->params[i]);
  398.     }
  399.     theField = *(paramPtr->params[0]);
  400.     theTarget = *(paramPtr->params[1]);
  401.     if (paramPtr->paramCount>2)  {
  402.         casePtr = *(paramPtr->params[2]);
  403.         strcpy(caseStr,casePtr);
  404.     }
  405.     else strcpy(caseStr,"");
  406.     for (i=0;i<strlen(caseStr);i++) caseStr[i] = toupper(caseStr[i]);
  407.     if (strcmp(caseStr,"TRUE")) theCase = 1;
  408.     else theCase = 0;
  409.     if (paramPtr->paramCount>3) {
  410.         theStart = *(paramPtr->params[3]);
  411.         strcpy(startStr,theStart);
  412.         CtoPstr(startStr);
  413.         StringToNum(startStr,&startAt);
  414.     }
  415.     else startAt = 0;
  416.         
  417.     FindIt(theField,theTarget,theCase,theResult,(int)startAt);
  418.     
  419.     /* assemble the return string */
  420.     
  421.     len = 1+strlen(theResult);
  422.     resultHandle = NewHandle(len);
  423.     HLock(resultHandle);
  424.     BlockMove(theResult,*resultHandle,len);
  425.     HUnlock(resultHandle);
  426.     paramPtr->returnValue = resultHandle;
  427.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  428.     return;
  429. }
  430.  
  431. pascal void main(paramPtr)
  432. XCmdBlockPtr    paramPtr;
  433. /* this is the entry point for the XFCN */
  434. {
  435.     FindInField(paramPtr);      /* run the main event loop */   
  436.     return;
  437. }
  438.  
  439.  
  440.  
  441. -- part contents for card part 9
  442. ----- text -----
  443. 135,7,2